/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

 #include "app_task.h"

 #include <zephyr/logging/log.h>
 #include <zephyr/drivers/uart.h>
 
 #define RECEIVE_BUFF_SIZE 10
 #define RECEIVE_TIMEOUT 100
 
 LOG_MODULE_REGISTER(app, CONFIG_CHIP_APP_LOG_LEVEL);
 const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart1));
 static uint8_t rx_buf[RECEIVE_BUFF_SIZE] = {0};
 
 
 
 static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
 {
     switch (evt->type) {
 
     case UART_RX_RDY:
         if ((evt->data.rx.len) == 1) {
 
         
 
             static uint8_t tx[] = {"yuezheng.gao get uart data\r\n"};
             uart_tx(uart, tx, sizeof(tx), SYS_FOREVER_US);
         }
         break;
     case UART_RX_DISABLED:
         uart_rx_enable(dev, rx_buf, sizeof rx_buf, RECEIVE_TIMEOUT);
         break;
 
     default:
         break;
     }
 }


 int main()
 {
     int ret;
     LOG_ERR("Hello World from LOG_ERR this it matter lock");
     printk(" Hello World from printk this it matter lock");

     if (!device_is_ready(uart))
     {
            printk("uart faile");
         return EXIT_FAILURE;
     }

    //  /*******串口手动配置（可以不手动配置，会使用默认配置，以下就是默认配置）************************/
     const struct uart_config uart_cfg = {
         .baudrate = 115200,
         .parity = UART_CFG_PARITY_NONE,
         .stop_bits = UART_CFG_STOP_BITS_1,
         .data_bits = UART_CFG_DATA_BITS_8,
         .flow_ctrl = UART_CFG_FLOW_CTRL_NONE};

     int errUart = uart_configure(uart, &uart_cfg);

     if (errUart == -ENOSYS)
     {
        printk("uart_configure faile");
         return -ENOSYS;
     }
    //  /******************************************************************************************/

     ret = uart_callback_set(uart, uart_cb, NULL);
     if (ret)
     {
        printk("uart_callback_set faile %d",ret);
         return 1;
     }

     ret = uart_rx_enable(uart, rx_buf, sizeof rx_buf, RECEIVE_TIMEOUT);
     if (ret)
     {
        printk("uart_rx_enable faile");
         return 1;
     }

     CHIP_ERROR err = AppTask::Instance().StartApp();

     LOG_ERR("Exited with code %" CHIP_ERROR_FORMAT, err.Format());
     return err == CHIP_NO_ERROR ? EXIT_SUCCESS : EXIT_FAILURE;
 }
